package cn.dgut.training.service.impl;

import cn.dgut.training.dto.UserDTO;
import cn.dgut.training.entity.user.User;
import cn.dgut.training.mapper.UserMapper;
import cn.dgut.training.service.IUserService;
import cn.dgut.training.vo.BaseVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    public BaseVO listUserByPage(UserDTO userDTO) {

        //构造分页
        IPage page = new Page();
        BeanUtils.copyProperties(userDTO, page);
        //获取条件查询构造器
        QueryWrapper queryWrapper = UserDTO.getWrapper(userDTO);

        IPage res = userMapper.selectPage(page, queryWrapper);

        BaseVO baseVO = new BaseVO(res);

        return baseVO;
    }

    public User getByOpenId(UserDTO userDTO) {

        //获取条件查询构造器
        QueryWrapper queryWrapper = UserDTO.getWrapper(userDTO);

        User user=userMapper.selectOne(queryWrapper);


        return user;
    }

}
